package com.zdxc.service;


import com.zdxc.dao.CodezyzMapper;
import com.zdxc.dao.UserinfoMapper;
import com.zdxc.entities.Userinfo;

import common.bean.Paging;
import common.dao.BaseDAO;
import common.dao.BaseMyBatisDAO;
import common.util.DateUtils;
import common.util.StringEncrypt;
import common.util.StringUtils;
import common.util.UserInfoUtils;
import org.apache.ibatis.jdbc.SQL;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 用户服务类
 */
@Lazy
@Service
public class UserService   {
    @Autowired
    BaseMyBatisDAO baseMyBatisDAO;
    private static final String SQL_NS = "com.zdxc.dao.Userinfo.";

    public Paging findByCondition(Userinfo userinfo, Paging paging) {
        paging.setStringParams(userinfo);

        paging = baseMyBatisDAO.findForPageHelper(SQL_NS + "findByCondition", paging);

        return paging;
    }

    /**
     * 根据userId查询用户信息
     *
     * @param userId
     * @return Userinfo
     */
    public Userinfo findByKey(String userId){
        return baseMyBatisDAO.findUnique(SQL_NS +"selectByPrimaryKey", userId);
    }


    /**
     * 根据userId删除用户
     *
     * @param userId
     * @return Userinfo
     */
    public int  delete(String userId){
        return baseMyBatisDAO.delete(SQL_NS +"deleteByPrimaryKey", userId);
    }

    /**
     * 根据userCode查询数据采集表
     *
     * @param userCode
     * @return Userinfo
     */
    public Userinfo findUserByUsercode(String userCode){
        return baseMyBatisDAO.findUnique(SQL_NS +"selectByUsercode", userCode);
    }


    /**
     * 插入用户信息
     * @param userInfo
     * @return int
     */
    public int insert(Userinfo userInfo){
        userInfo.setUserId(StringUtils.getUUID()); //生成主键
        userInfo.setPasswd(StringEncrypt.encryptByMd5(userInfo.getPasswd())); //密码加密
        userInfo.setNickname(userInfo.getUserName());
        userInfo.setCreationDate(new Date());  //当前时间
        userInfo.setLastUpdateDate(userInfo.getCreationDate());
        userInfo.setLastUpdatedBy(UserInfoUtils.getUser().getUserCode());
        userInfo.setRecordStatus(1) ;                 //有效标志
        return baseMyBatisDAO.insert(SQL_NS +"insert", userInfo);
    }


    /**
     * 更新用户信息
     *
     */
    public int update(Userinfo userinfo)
    {
      int result  =   baseMyBatisDAO.update(SQL_NS+"updateByPrimaryKey",userinfo);
      return result;
    }

    /**
     * 修改密码
     */
    public int modifyPass(String userId , String password )
    {

        Userinfo userinfoEntity  =   this.findByKey(userId);
        userinfoEntity.setPasswd(StringEncrypt.encryptByMd5(password));
        return update(userinfoEntity);
    }


}
